package com.rykj.energy.common.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author jixiang
 * <p>
 * <p>
 * 时间格式转化工具
 */
public class DateUtils {
    public final static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    public final static String DATE_FORMAT_SIMAPLE_HM = "yyyy-MM-dd HH:mm";
    public final static String DATE_FORMAT_SIMAPLE = "yyyy-MM-dd";
    public final static String DATE_FORMAT_SIMAPLE_CH = "yyyy年MM月dd日";
    public final static String DATE_FORMAT_DAY = "yyyy-MM-dd";
    public final static String DATE_FORMAT_MONTH = "yyyy-MM";
    public final static String DATE_DEFAULT = "1970-01-01 12:00:00";
    public final static String DATE_FORMAT_HOURS = "HH";
    public final static String DATE_F_DAY = "yyyyMMdd";
    //ISO8601 标准中的时间格式 yyyy-MM-dd’T’HH:mm:ss.SSSXXX
    public final static String DATE_FORMAT_ISO ="yyyy-MM-dd'T'HH:mm:ss.SSSXXX";


    /**
     * @param ts 要传入字符串类型的时间戳格式为XXXX-XX-XX HH:MM:SS
     */
    public static long getTimeStamp(String ts) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
        Date start = sdf.parse(ts);
        return start.getTime();
    }

    /**
     * 将long型的时间戳转换为YYYY-MM-DD HH:MM:SS格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDate(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }

    /**
     * 将long型的时间戳转换为YYYY-MM-DD格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDateDay(long timestamp) {
        Date dt = new Date(timestamp);
        return getDateDay(dt);
    }

    /**
     * 将long型的时间戳转换为YYYYMMDD格式的日期字符串
     *
     * @param date
     * @return date_string
     */
    public static String getDateToDay(Date date) {
        if(date == null) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_F_DAY);
        return dateFormat.format(date);
    }
    
    /**
     * 将long型的时间戳转换为YYYY-MM-DD格式的日期字符串
     *
     * @param date
     * @return date_string
     */
    public static String getDateDay(Date date) {
        if(date == null) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
        return dateFormat.format(date);
    }

    /**
     * 将long型的时间戳转换为YYYY-MM格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDateMonth(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_MONTH);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }

    /**
     * 将long型的时间戳转换为HH:MM格式的日期字符串
     *
     * @param timestamp
     * @return date_string
     */
    public static String getDateHours(long timestamp) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_HOURS);
        Date dt = new Date(timestamp);
        return dateFormat.format(dt);
    }


    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月18日 下午3:08:05<br/>
     * Version:1.0 <br/>
     *
     * @param date
     * @return 数据格式为'yyyy-MM-dd hh:mm:ss' 时间
     */
    public static String getDate(Date date) {
        return getDate(date, DATE_FORMAT);
    }

    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014-9-12 下午2:18:21<br/>
     * Version:1.0 <br/>
     *
     * @param date    需格式化的时间
     * @param pattern 时间格式
     * @return 返回为pattern的时间
     */
    public static String getDate(Date date, String pattern) {
        if (ObjectUtils.isEmpty(date)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月18日 下午3:08:05<br/>
     * Version:1.0 <br/>
     *
     * @param date 需格式化时间,默认格式为 'yyyy-MM-dd hh:mm:ss'
     * @return
     * @throws ParseException
     */
    public static Date getDate(String date) throws ParseException {
        return getDate(date, DATE_FORMAT);
    }

    /**
     * 方法描述:通过date获取时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月18日 下午3:08:05<br/>
     * Version:1.0 <br/>
     *
     * @param date    需格式化时间,默认格式为 'yyyy-MM-dd hh:mm:ss'
     * @param pattern 时间格式
     * @return
     * @throws ParseException
     */
    public static Date getDate(String date, String pattern) throws ParseException {
        if (StringUtils.isEmpty(date)) {
            return null;
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(StringUtils.isEmpty(pattern) ? DATE_FORMAT : pattern);
        return dateFormat.parse(date);
    }

    /**
     * 方法描述:获取当前时间<br/>
     * Author:<a href="lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月11日 上午11:10:40<br/>
     * Version:1.0 <br/>
     *
     * @return 当前时间格式:pattern
     */
    public static String getNowDate(String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(new Date());
    }

    /**
     * 方法描述:获取当前时间<br/>
     * Author:<a href="lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2014年8月11日 上午11:10:40<br/>
     * Version:1.0 <br/>
     *
     * @return 当前时间格式"yyyy-MM-dd hh:mm:ss"
     */
    public static String getNowDate() {
        return getNowDate(DATE_FORMAT);
    }

    /**
     * 添加多少年
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowYear(Date now, int amount) {
        return addHow(now, Calendar.YEAR, amount);
    }

    /**
     * 添加多少月
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowMonth(Date now, int amount) {
        return addHow(now, Calendar.MONTH, amount);
    }

    /**
     * 添加多少周
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowWeek(Date now, int amount) {
        return addHow(now, Calendar.WEEK_OF_MONTH, amount);
    }

    /**
     * 添加多少天
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowDay(Date now, int amount) {
        return addHow(now, Calendar.DAY_OF_MONTH, amount);
    }

    /**
     * 添加多少小时
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowHour(Date now, int amount) {
        return addHow(now, Calendar.HOUR_OF_DAY, amount);
    }

    /**
     * 添加多少分钟
     *
     * @param now
     * @param amount
     * @return
     */
    public static Date addHowMinute(Date now, int amount) {
        return addHow(now, Calendar.MINUTE, amount);
    }

    public static Date addHowSecond(Date now, int amount) {
        return addHow(now, Calendar.SECOND, amount);
    }

    /**
     * 在当前时间添加多少数量
     *
     * @param field
     * @param amount
     * @return
     */
    public static Date addHow(int field, int amount) {
        return addHow(new Date(), field, amount);
    }

    public static Date addHow(Date now, int field, int amount) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(now);
        calendar.add(field, amount);
        return calendar.getTime();
    }

    public static Date parseDate(String date, String pattern) {
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            return dateFormat.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    public static int getWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.WEEK_OF_YEAR);
    }

    public static int getYear(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.YEAR);
    }

    public static int getMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MONTH) + 1;
    }

    public static int getDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    public static int getHour(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static int getMinute(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.MINUTE);
    }

    public static int getSecond(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.SECOND);
    }

    /**
     * 获取周时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInWeek(Date date) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置周
        calendar.setTime(date);
        //设置为星期一(一周第一天是星期天所以星期一就是2)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        res[0] = calendar.getTime();
        //设置为星期天(在当前时间往后加6天就是星期天)
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        res[1] = calendar.getTime();
        return res;
    }

    /**
     * 获取周范围时间段的起止时间
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInWeek(Date date, int week) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置周
        calendar.setTime(date);
        //设置为星期一(一周第一天是星期天所以星期一就是2)
        calendar.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        //设置为星期天(在当前时间往后加6天就是星期天)
        calendar.add(Calendar.DAY_OF_YEAR, 6);
        res[1] = calendar.getTime();
        calendar.add(Calendar.DAY_OF_YEAR, 7 * week);
        res[0] = calendar.getTime();
        return res;
    }

    /**
     * 获取月时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInMonth(Date date) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置
        calendar.setTime(date);
        //设置为一号
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        res[0] = calendar.getTime();
        //设置为最后一天
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        res[1] = calendar.getTime();
        return res;
    }

    /**
     * 获取月时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInMonth(Date date, int month) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置
        calendar.setTime(date);
        //设置为一号
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        res[0] = calendar.getTime();
        calendar.add(Calendar.MONTH, month);
        //设置为最后一天
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        res[1] = calendar.getTime();
        return res;
    }

    /**
     * 获取年时间段
     *
     * @param date
     * @return
     */
    public static Date[] getFragmentInYear(Date date) {
        Date[] res = new Date[2];
        Calendar calendar = Calendar.getInstance();
        //设置
        calendar.setTime(date);
        //设置为第一天
        calendar.set(Calendar.DAY_OF_YEAR, 1);
        res[0] = calendar.getTime();
        //设置为最后一天
        calendar.add(Calendar.YEAR, 1);
        calendar.add(Calendar.DAY_OF_YEAR, -1);
        res[1] = calendar.getTime();
        return res;
    }

    public static boolean equals(Date firstDate, Date secondDate) {
        String firstStr = getDate(firstDate);
        String secondStr = getDate(secondDate);
        return firstStr.equals(secondStr);
    }

    /**
     * 获得指定日期所在周- 周一  的日期
     * @param date
     * @return
     */
    public static String getWeekOfMonday(Date date) {
        int mondayPlus = getMondayPlus(date);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.add(GregorianCalendar.DATE, mondayPlus);
        Date monday = currentDate.getTime();
        String preMonday = getDate(monday, DateUtils.DATE_FORMAT_DAY);
        return preMonday;
    }

    /**
     * 获得指定日期所在周- 周日  的日期
     * @param date
     * @return
     */
    public static String getWeekOfSunday(Date date) {
        int mondayPlus = getMondayPlus(date);
        GregorianCalendar currentDate = new GregorianCalendar();
        currentDate.setTime(date);
        currentDate.add(GregorianCalendar.DATE, mondayPlus + 6);
        Date monday = currentDate.getTime();
        String preMonday = getDate(monday, DateUtils.DATE_FORMAT_DAY);
        return preMonday;
    }

    /**
     * 获得指定日期与当时周一相差的天数
     * @param date
     * @return
     */
    public static int getMondayPlus(Date date) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cd.get(Calendar.DAY_OF_WEEK);
        if (dayOfWeek == 1) {
            return -6;
        } else {
            return 2 - dayOfWeek;
        }
    }

    /**
     * 方法描述:获取当前时间前后指定时间<br/>
     * Author:<a href="mailto:lilong@tsinghuabigdata.com">lilong</a> <br/>
     * Date:2015-8-12 下午2:13:42<br/>
     * Version:1.0 <br/>
     *
     * @param day
     * @return
     */
    public static Date getDayDate(int day) {
        Calendar cd = Calendar.getInstance();
        cd.add(Calendar.DAY_OF_MONTH, day);
        return cd.getTime();
    }

//    /**
//     * 获取当天0点时间戳
//     *
//     * @return
//     */
//    public static Timestamp getDayBegin() {
//        Calendar cal = Calendar.getInstance();
//        cal.set(Calendar.HOUR_OF_DAY, 0);
//        cal.set(Calendar.SECOND, 0);
//        cal.set(Calendar.MINUTE, 0);
//        cal.set(Calendar.MILLISECOND, 000);
//        return new Timestamp(cal.getTimeInMillis());
//    }
    
    /**
     * 获取当天0点时间戳
     *
     * @return
     */
    public static Date getDayBegin(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.MILLISECOND, 000);
        return cal.getTime();
    }
    
    /**
     * 获取当天23:59:59点时间戳
     *
     * @return
     */
    public static Date getDayEnd(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.MILLISECOND, 999);
        return cal.getTime();
    }
    
    
    public static final long setTime(Date date, Integer week) {
        long result = 0;
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            
            result = cal.getTimeInMillis();
        } catch (Exception e) {
            return result;
        }       
        return result;
    }
            
    public static final Date setTime(Date date, Integer year, Integer month, Integer day,
        Integer hour, Integer minute, Integer second, Integer week) {
        Date result = null;
        if(day !=null && week!= null) {
            throw new IllegalArgumentException("天和星期不能同时存在");
        }
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            if (year != null) {
                cal.set(Calendar.YEAR, year);
            }
            if (month != null) {
                cal.set(Calendar.MONTH, month-1);
            }
            if (day != null) {
                cal.set(Calendar.DAY_OF_MONTH, day);
            }
            if (hour != null) {
                cal.set(Calendar.HOUR_OF_DAY, hour);
            }
            if (minute != null) {
                cal.set(Calendar.MINUTE, minute);
            }
            if (second != null) {
                cal.set(Calendar.SECOND, second);
            }
            if (week != null) {
                cal.set(Calendar.DAY_OF_WEEK, week);
            }
            result = cal.getTime();
        } catch (Exception e) {
            return result;
        }       
        return result;
    }
    
    
    /**
     * 获取当天0点时间戳
     *
     * @return
     */
    public static Date getDayBegin() {
        return getDayBegin(new Date());
    }
    
    /**
     * 获取当天23:59:59点时间戳
     *
     * @return
     */
    public static Date getDayEnd() {
        return getDayEnd(new Date());
    }

    /**
     * 判断当前时间时候在低频检测时间内，是：返回开始、结束时间  否：返回null
     */
    public static  Map<String,Long> checkFrequencyTime(String startTime,String endTime){
        Map<String,Long> map = null;
        if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            JSONArray startArray = JSONArray.parseArray(startTime);
            JSONArray endArray = JSONArray.parseArray(endTime);
            for (int i = 0; i < startArray.size(); i++) {
                JSONObject startTimeI = ((JSONObject) startArray.get(i)).getJSONObject("time");
                Integer startHourI = Integer.valueOf(startTimeI.getString("hour"));
                Integer startMinuteI = Integer.valueOf(startTimeI.getString("minute"));
                Integer startSecondI = Integer.valueOf(startTimeI.getString("second"));
                JSONObject endTimeI = ((JSONObject) endArray.get(i)).getJSONObject("time");
                Integer endHourI = Integer.valueOf(endTimeI.getString("hour"));
                Integer endMinuteI = Integer.valueOf(endTimeI.getString("minute"));
                Integer endSecondI = Integer.valueOf(endTimeI.getString("second"));
                long startlong = DateUtils.getDayBegin().getTime() + startHourI*3600*1000 + startMinuteI*60*1000 + startSecondI*1000;
                long endlong = DateUtils.getDayBegin().getTime() + endHourI*3600*1000 + endMinuteI*60*1000 + endSecondI*1000;

                long nowlong= new Date().getTime();
                String cycleType = ((JSONObject) startArray.get(i)).getString("cycleType");
                if(cycleType.equals("week")){
                    JSONArray weeks = JSONArray.parseArray(startTimeI.getString("weeks"));
                    int week = DateUtils.getWeek(DateUtils.getDateDay(new Date()));
                    if(week == 0){  //星期天
                        week = 7;
                    }
                    if(!weeks.contains(week)){  //不在执行时间内
                        return map;
                    }
                }
                if(startlong<nowlong && nowlong<endlong){   //需要执行任务
                    map = new HashMap<>();
                    map.put("startlong",startlong);
                    map.put("endlong",endlong);
                }
            }
        }
        return map;
    }

    /**
     * 传入两个字符串  示例  02:00:00    17:00:00
     * 判断当前时间是否在时间段内   示例 true 2019-03-21 10:25:31   false 2019-03-21 19:25:31
     * @return
     */
    public static Boolean getTimeSlot(String start,String end) {
        Boolean b = false;
        long now = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
        String day = dateFormat.format(now);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = day+" "+start;
        String endTime = day+" "+end;
        Date startDate = null;
        Date endDate = null;
        long startLong = 0;
        long endLong = 0;
        try {
            startDate = simpleDateFormat.parse(startTime);
            endDate = simpleDateFormat.parse(endTime);
            startLong = startDate.getTime();
            endLong = endDate.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //跨天
        if(endLong<startLong){  
            if(now > startLong){
                b=true;
            }
        }else {
            if(now>startLong && now <endLong){
                b=true;
                //在时间段内
            }else if(now<startLong){
                //时间段前
            }else if(now>endLong){
                //时间段后
            }
        }
        return b;
    }

    /**
     * 获取字符串今天的时间 示例 09:30:30
     * @return  2019-03-20 09:30:30
     */
    public static Date getDateByString(String str) {
        long now = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
        String day = dateFormat.format(now);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = day+" "+str;
        Date date = null;
        try {
            date = simpleDateFormat.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getStringByString(String str) {
        long now = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
        String day = dateFormat.format(now);
        String strTime = day+" "+str;
        return strTime;
    }

    /**
     * 输入一个字符串  如15:00:00  判断当前时间在这前或者后  返回日期
     * 如 输入15:00:00  当前时间为  2019-03-22 17:26:27   返回2019-03-23
     *    输入15:00:00  当前时间为  2019-03-22 11:26:27   返回2019-03-22
     */
    public static String getDayByStr(String str) {
        long now = System.currentTimeMillis();
        SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT_DAY);
        String day = dateFormat.format(now);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String strTime = day+" "+str;
        Date date = null;
        try {
            date = simpleDateFormat.parse(strTime);
            if(now>date.getTime()){
                long sss = now+24*60*60*1000;
                day = dateFormat.format(sss);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;
    }

    /**
     * 计算两个时间相差多少小时
     * 不足1小时的丢弃
     */
    public static int getHourDiffer(Date firstDate,Date lastDate) {
        long nh = 1000 * 60 * 60;
        // 获得两个时间的毫秒时间差异
        long diff = lastDate.getTime() - firstDate.getTime();
        // 计算差多少小时
        long hour = diff / nh;
        return (int)hour;
    }



    /**
    * @Title: isAfter 
    * @Description: 判断第一个时间是否在第二个时间之后
    * @param firstDate 
    * @param lastDate
    * @return boolean    返回类型 
    * @throws
     */
    public static boolean isAfter(Date firstDate,Date lastDate) {
        return firstDate.getTime() - lastDate.getTime()>0; 
    }

    /**
     * @Description: 判断两个时间段是否有重合
     * @param startTime
     * @param endTime
     * @return boolean    返回类型
     */
    public static boolean checkTime(String startTime, String endTime) throws Exception{
        boolean b = true;
        if(StringUtils.isNotEmpty(startTime) && StringUtils.isNotEmpty(endTime)) {
            JSONArray startArray = JSONArray.parseArray(startTime);
            JSONArray endArray = JSONArray.parseArray(endTime);
            for (int i = 0; i < startArray.size(); i++) {
                JSONObject startTimeI = ((JSONObject) startArray.get(i)).getJSONObject("time");
                Integer startHourI = Integer.valueOf(startTimeI.getString("hour"));
                Integer startMinuteI = Integer.valueOf(startTimeI.getString("minute"));
                Integer startSecondI = Integer.valueOf(startTimeI.getString("second"));
                JSONObject endTimeI = ((JSONObject) endArray.get(i)).getJSONObject("time");
                Integer endHourI = Integer.valueOf(endTimeI.getString("hour"));
                Integer endMinuteI = Integer.valueOf(endTimeI.getString("minute"));
                Integer endSecondI = Integer.valueOf(endTimeI.getString("second"));

                for (int j = i + 1; j < startArray.size(); j++) {
                    JSONObject startTimeJ = ((JSONObject) startArray.get(j)).getJSONObject("time");
                    Integer startHourJ = Integer.valueOf(startTimeJ.getString("hour"));
                    Integer startMinuteJ = Integer.valueOf(startTimeJ.getString("minute"));
                    Integer startSecondJ = Integer.valueOf(startTimeJ.getString("second"));
                    JSONObject endTimeJ = ((JSONObject) endArray.get(j)).getJSONObject("time");
                    Integer endHourJ = Integer.valueOf(endTimeJ.getString("hour"));
                    Integer endMinuteJ = Integer.valueOf(endTimeJ.getString("minute"));
                    Integer endSecondJ = Integer.valueOf(endTimeJ.getString("second"));

                    int startTimeSecondI = startHourI * 3600 + startMinuteI * 60 + startSecondI;
                    int endTimeSecondI = endHourI * 3600 + endMinuteI * 60 + endSecondI;
                    int startTimeSecondJ = startHourJ * 3600 + startMinuteJ * 60 + startSecondJ;
                    int endTimeSecondJ = endHourJ * 3600 + endMinuteJ * 60 + endSecondJ;
                    if (!(endTimeSecondJ < startTimeSecondI) && !(startTimeSecondJ > endTimeSecondI)) {
                        b = false;
                    }
                }
            }
        }
        return b;
    }

    /**
     * 计算hourNum小时内的整点时间
     * @param hourNum
     * @return
     */
   public static List<Map<String,Object>> getDateHoursByHourNum(Integer hourNum){
      List<Map<String,Object>> hourMaps = new ArrayList<>();
        for (int i=0;i<=hourNum;i++){
            Calendar calendar = Calendar.getInstance();
            calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - (hourNum-i));
            SimpleDateFormat df = new SimpleDateFormat("HH");
            String  countTime = df.format(calendar.getTime());
            Map<String,Object> map = new HashMap<>();
            map.put("countNum",0);
            map.put("countTime",countTime);
            hourMaps.add(map);
        }
        return  hourMaps;
   }

    /**
     * String "yyyy-MM-dd"
     */
    public static int getWeek (String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(format.parse(date));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return calendar.get(Calendar.DAY_OF_WEEK)-1;
    }

    public static void main(String[] args) throws ParseException {
        System.out.println(DateUtils.getDateToDay(new Date()));
    }

    public static String getISOTime(Date beginDate) {
        SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_ISO);
        String isoTime = sdf.format(beginDate);
        return isoTime;
    }
}
